﻿// <copyright file="Media.cs" company="Microsoft Corporation">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2008-09-26</date>
// <summary>Photobucket Media</summary>

namespace Photobucket
{
    using System;
    using System.Text;
    using System.Windows.Media.Imaging;
    using System.Xml.Linq;

    /// <summary>
    /// Photobucket Media
    /// </summary>
    /// <remarks>Ignore CA1724: Microsoft.Naming code analysis warning</remarks>
    public class Media
    {
        /// <summary>
        /// Initializes a new instance of the Media class.
        /// </summary>
        public Media()
        {
        }

        /// <summary>
        /// Initializes a new instance of the Media class.
        /// </summary>
        /// <param name="media">xml media element</param>
        public Media(XElement media)
        {
            this.DescriptionId = media.Attribute("description_id").Value;
            this.MediaType = media.Attribute("type").Value;
            this.Name = media.Attribute("name").Value;
            this.Public = int.Parse(media.Attribute("public").Value, System.Globalization.CultureInfo.InvariantCulture);
            this.UserName = media.Attribute("username").Value;
            this.UploadDate = Synergist.Utility.UnixTimeToDateTime(media.Attribute("uploaddate"));
            this.BrowseUrl = new Uri(media.Element("browseurl").Value);
            this.Url = new Uri(media.Element("url").Value);
            this.Thumb = new Uri(media.Element("thumb").Value);
            this.Description = media.Element("description").Value;
            this.Title = media.Element("title").Value;
        }

        /// <summary>
        /// Gets or sets Media Description ID
        /// </summary>
        public string DescriptionId { get; set; }

        /// <summary>
        /// Gets or sets Media Name
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets Public
        /// </summary>
        public int Public { get; set; }

        /// <summary>
        /// Gets or sets Media Type
        /// </summary>
        public string MediaType { get; set; }

        /// <summary>
        /// Gets or sets User Name
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// Gets or sets Browse URL
        /// </summary>
        public Uri BrowseUrl { get; set; }

        /// <summary>
        /// Gets or sets Image URL
        /// </summary>
        public Uri Url { get; set; }

        /// <summary>
        /// Gets the bitmap image
        /// </summary>
        public BitmapImage Image
        {
            get
            {
                if (this.Url == null)
                {
                    return null;
                }

                return new BitmapImage(this.Thumb);
            }
        }

        /// <summary>
        /// Gets or sets Album URL
        /// </summary>
        public Uri AlbumUrl { get; set; }

        /// <summary>
        /// Gets or sets Thumbnail image
        /// </summary>
        public Uri Thumb { get; set; }

        /// <summary>
        /// Gets the thumbnail image
        /// </summary>
        public BitmapImage ThumbImage 
        {
            get
            {
                if (this.Thumb == null)
                {
                    return null;
                }

                return new BitmapImage(this.Thumb);
            }
        }

        /// <summary>
        /// Gets or sets Description
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets Media Title
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets Upload date
        /// </summary>
        public DateTime UploadDate { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether the media is sponsored
        /// </summary>
        public bool IsSponsored { get; set; }

        /// <summary>
        /// Output the property names and values
        /// </summary>
        /// <returns>a string formatted the property names and values</returns>
        public override string ToString()
        {
            return Synergist.Utility.GetProperties(this);
        }
    }
}
